<?php

namespace app\app\controller;

use app\app\model\ProjectDKModel;
use app\BaseController;
use app\app\model\ProjectModel;
use app\app\model\ProjectRiskModel;
use app\Request;

class ProjectRisk extends BaseController
{

    public function analysis()
    {
        $type = input('type', '0');
        $time = $this->timeArr($type);
        $data['start_time'] = input('start_time', '');
        $data['end_time'] = input('end_time', '');
        if (!empty($data['start_time']) || !empty($data['end_time'])) {
            if (!empty($data['start_time']) && !empty($data['end_time'])) {
                $where[] = ['start_time', '<=', $data['end_time']];
                $where[] = ['end_time', '>=', $data['start_time']];
            } else {
                if (!empty($data['start_time'])) $where[] = ['start_time', '>', $data['start_time']];
                if (!empty($data['end_time'])) $where[] = ['start_time', '<', $data['end_time']];
            }
        } else {
            $where[] = ['start_time', '<=', $time['end_time']];
            $where[] = ['end_time', '>=', $time['start_time']];
        }
//        if(!empty($data['start_time']) || !empty($data['end_time'])){
//            $where[] = ['start_time_real','<=',$time['end_time']];
//        }else{
//            $where[] = ['start_time_real','<=',$time['end_time']];
//        }


        $risk_num = ProjectRiskModel::where($where)
            ->where('risk_level', '<=', 3)->count();
        //三级风险
        $risk_num_3 = ProjectRiskModel::where($where)
            ->where('risk_level', '=', 3)->count();
        //二级风险
        $risk_num_2 = ProjectRiskModel::where($where)->where('risk_level', '=', 2)->count();


        $risk_ratio_2 = '-';
        $risk_ratio_3 = '-';
        if ($type == 2) {
            //当天风险打卡条件
            $where_dk[] = ['dk_time_in', 'between', [$time['start_time'], $time['end_time']]];

            //当天二级风险数的项目
            $risk_list_2 = ProjectRiskModel::where($where)
                ->where('risk_level', '=', 2)
                ->group('project_name')
                ->select()->toArray();
            $info_2 = array_column($risk_list_2, 'project_id');
            $ids_2 = implode(",", $info_2);
            $where_risk_2[] = ['project_id', 'in', $ids_2];
            //当天二级风险打卡数
            $risk_dk_2 = ProjectDKModel::where('person_type','=',1)
                ->where($where_risk_2)->where($where_dk)->group('project_id,user_id')->count();
            //二级风险打卡履职率
            $risk_ratio_2 = '-';
            if ($risk_num_2) {
                $risk_ratio_2 = number_format($risk_dk_2 / $risk_num_2, 2) * 100 . '%';
            }


            //当天三级级风险数的项目
            $risk_list_3 = ProjectRiskModel::where($where)
                ->where('risk_level', '=', 3)
                ->group('project_name')
                ->select()->toArray();
            $info_3 = array_column($risk_list_3, 'project_id');
            $ids_3 = implode(",", $info_3);
            $where_risk_3[] = ['project_id', 'in', $ids_3];
            //当天三级风险打卡数
            $risk_dk_3 = ProjectDKModel::where('person_type','=',1)
                ->where($where_risk_3)->where($where_dk)->group('project_id,user_id')->count();
            //三级风险打卡履职率

            $risk_ratio_3 = '-';
            if ($risk_num_3) {
                $risk_ratio_3 = number_format($risk_dk_3 / $risk_num_3, 2) * 100 . '%';
            }
        }

        $res = array(
            'risk_num' => $risk_num,
            'risk_num_3' => $risk_num_3,
            'risk_num_2' => $risk_num_2,
            'work_count' => 111,
            'risk_ratio_2' => $risk_ratio_2,
            'risk_ratio_3' => $risk_ratio_3,
            'people_num' => 4805,
        );
        $this->success('success', $res);
    }

    public function index(Request $request)
    {
        $data = $request->param();
        $page_size = input('pageSize', '20');
        $type = input('type', '0');
        $where[] = ['a.is_parent', '=', 1];
        $state = input('state', '1');
        if ($state == 1) {
            $where[] = ['c.risk_level', '<=', 3];
        } else {
            if (in_array($state, [2, 4])) {
                $risk_state = 2;
            } else {
                $risk_state = 3;
            }
            $where[] = ['c.risk_level', '=', $risk_state];

        }
        //城市查询
        if (isset($data['city']) && !empty($data['city'])) {
            $city_arr = explode(',', $data['city']);
            $where[] = ['a.city', 'IN', $city_arr];;
        }

        //项目名查询
        if (isset($data['project_name'])) {
            $where[] = ['b.project_name|a.project_name', 'like', '%' . $data['project_name'] . '%'];
        }

        $time = $this->timeArr($type);
        $data['start_time'] = input('start_time', '');
        $data['end_time'] = input('end_time', '');

        if (!empty($data['start_time']) || !empty($data['end_time'])) {
            if (!empty($data['start_time']) && !empty($data['end_time'])) {
                $where[] = ['c.start_time', '<=', $data['end_time']];
                $where[] = ['c.end_time', '>=', $data['start_time']];
            } else {
                if (!empty($data['start_time'])) $where[] = ['c.start_time', '>', $data['start_time']];
                if (!empty($data['end_time'])) $where[] = ['c.start_time', '<', $data['end_time']];
            }
        } else {
            $where[] = ['c.start_time', '<=', $time['end_time']];
            $where[] = ['c.end_time', '>=', $time['start_time']];
        }


//        if ($type == 2) {
//            $where[] = ['start_time_real', '<=', $time['end_time']];
//        } else {
//            if (!empty($data['start_time']) || !empty($data['end_time'])) {
//                if (!empty($data['start_time']) && !empty($data['end_time'])) {
//                    $where[] = ['start_time', '<=', $data['end_time']];
//                    $where[] = ['end_time', '>=', $data['start_time']];
//                } else {
//                    if (!empty($data['start_time'])) $where[] = ['start_time', '>', $data['start_time']];
//                    if (!empty($data['end_time'])) $where[] = ['start_time', '<', $data['end_time']];
//                }
//            } else {
//                $where[] = ['start_time', '<=', $time['end_time']];
//                $where[] = ['end_time', '>=', $time['start_time']];
//            }
//        }

        $list = ProjectModel::alias('a')
            ->field('b.id,b.project_name,COUNT(b.id) as total')
            ->leftjoin('t_project_risk c', 'a.id = c.project_id')
            ->leftjoin('t_project b', 'a.link_project_id = b.id')
            ->where($where)
            ->group('b.project_name')
            ->paginate($page_size);
        $this->success('success', $list);
    }

    public function riskList()
    {
        $id = input('project_id', '');
        $type = input('type', '1');
        $where[] = ['b.link_project_id', '=', $id];
        $state = input('state', '1');

        $time = $this->timeArr($type);
        $data['start_time'] = input('start_time', '');
        $data['end_time'] = input('end_time', '');

        if (!empty($data['start_time']) || !empty($data['end_time'])) {
            if (!empty($data['start_time']) && !empty($data['end_time'])) {
                $where[] = ['a.start_time', '<=', $data['end_time']];
                $where[] = ['a.end_time', '>=', $data['start_time']];
            } else {
                if (!empty($data['start_time'])) $where[] = ['a.start_time', '>', $data['start_time']];
                if (!empty($data['end_time'])) $where[] = ['a.start_time', '<', $data['end_time']];
            }
        } else {
            $where[] = ['a.start_time', '<=', $time['end_time']];
            $where[] = ['a.end_time', '>=', $time['start_time']];
        }


//        if ($type == 2) {
//            $where[] = ['start_time_real', '<=', $time['end_time']];
//        } else {
//            if (!empty($data['start_time']) || !empty($data['end_time'])) {
//                if (!empty($data['start_time']) && !empty($data['end_time'])) {
//                    $where[] = ['start_time', '<=', $data['end_time']];
//                    $where[] = ['end_time', '>=', $data['start_time']];
//                } else {
//                    if (!empty($data['start_time'])) $where[] = ['start_time', '>', $data['start_time']];
//                    if (!empty($data['end_time'])) $where[] = ['start_time', '<', $data['end_time']];
//                }
//            } else {
//                $where[] = ['start_time', '<=', $time['end_time']];
//                $where[] = ['end_time', '>=', $time['start_time']];
//            }
//        }

        if ($state == 1) {
            $where[] = ['a.risk_level', '<=', 3];
        } else {

            if (in_array($state, [2, 4])) {
                $risk_state = 2;
            } else {
                $risk_state = 3;
            }
            $where[] = ['a.risk_level', '=', $risk_state];

//            $where[] = ['a.risk_level', '=', $state];
        }

        $list = ProjectRiskModel::alias('a')
            ->field('a.project_id,a.project_name,COUNT(a.project_id) as total')
            ->leftjoin('t_project b', 'a.project_id = b.id')
            ->leftjoin('t_project c', 'c.link_project_id = b.id')
            ->where($where)
            ->group('a.project_name')
            ->select();
        $this->success('success', $list);
    }


    public function risks()
    {
        $id = input('id', '');
        $type = input('type', '1');
        $where[] = ['project_id', '=', $id];

        $time = $this->timeArr($type);
        $data['start_time'] = input('start_time', '');
        $data['end_time'] = input('end_time', '');

        if (!empty($data['start_time']) || !empty($data['end_time'])) {
            if (!empty($data['start_time']) && !empty($data['end_time'])) {
                $where[] = ['start_time', '<=', $data['end_time']];
                $where[] = ['end_time', '>=', $data['start_time']];
            } else {
                if (!empty($data['start_time'])) $where[] = ['start_time', '>', $data['start_time']];
                if (!empty($data['end_time'])) $where[] = ['start_time', '<', $data['end_time']];
            }
        } else {
            $where[] = ['start_time', '<=', $time['end_time']];
            $where[] = ['end_time', '>=', $time['start_time']];
        }

        $state = input('state', '1');


        if ($type == 2) {
            if ($state > 3) {
                $where_list[] = ['dk_time_in', 'between', [$time['start_time'], $time['end_time']]];
                $where_list[] = ['project_id', '=', $id];
                $list = ProjectDKModel::where($where_list)->select();
            } else {
                if ($state == 1) {
                    $where[] = ['risk_level', '<=', 3];
                } else {
                    $where[] = ['risk_level', '=', $state];
                }
                $list = ProjectRiskModel::where($where)->select();
            }
        } else {
            if ($state == 1) {
                $where[] = ['risk_level', '<=', 3];
            } else {
                $where[] = ['risk_level', '=', $state];
            }
            $list = ProjectRiskModel::where($where)->select();
        }

        $this->success('success', $list);
    }

    public function info()
    {
        $id = input('id', '');
        $list = ProjectRiskModel::where('id', $id)->select();
        $this->success('success', $list);
    }


    /**
     * 获取单体项目风险个数
     */
    public function riskTotal()
    {
        $project_id = input('project_id');
        $where[] = ['project_id', '=', $project_id];
        $risk_num = ProjectRiskModel::where($where)
            ->where('risk_level', '<=', 3)->count();
        //三级风险
        $risk_num_3 = ProjectRiskModel::where($where)
            ->where('risk_level', '=', 3)->count();
        //二级风险
        $risk_num_2 = ProjectRiskModel::where($where)
            ->where('risk_level', '=', 2)->count();
        $res = array(
            'risk_num' => $risk_num,
            'risk_num_3' => $risk_num_3,
            'risk_num_2' => $risk_num_2,
        );
        $this->success('success', $res);

    }

    /**
     * 工作台根据状态获取风险数据
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function projectRiskList()
    {
        $project_id = input('project_id');
        $type = input('type', '1');
        $page_size = input('pageSize', '20');
        $where[] = ['project_id', '=', $project_id];

        switch ($type) {
            case 1:
                $where[] = ['lc_status', '=', '进行中'];
                break;
            case 2:
                $where[] = ['lc_status', '=', '已结束'];
                break;
            case 3:
                $where[] = ['lc_status', '=', '待开工'];
                break;
            case 4:
                $where[] = ['lc_status', '=', '暂停中'];
                break;
            default:
                break;
        }
        $list = ProjectRiskModel::where($where)->paginate($page_size);

        $this->success('success', $list);
    }


    public function update()
    {
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        ProjectRiskModel::where('id', $id)->update($data);
        $this->success('success');
    }


    public function timeArr($type)
    {
        if ($type == 1) { //本年
            $start_time = date('Y-01-01 00:00:00', time());
            $end_time = date('Y-12-31 23:59:59', time());
        } elseif ($type == 3) { //本周
            $start_time = date('Y-m-d 00:00:00', strtotime("this week Monday", time()));
            $end_time = date("Y-m-d 23:59:59", strtotime("$start_time +6 day"));
        } elseif ($type == 4) { //下周
            $start_time = date("Y-m-d 00:00:00", strtotime('next monday'));
            $end_time = date("Y-m-d 00:00:00", strtotime('next sunday'));
        } else {  //当天
            $start_time = date('Y-m-d 00:00:00', time());
            $end_time = date('Y-m-d 23:59:59', time());
        }
        $timeArray = [
            'start_time' => $start_time,
            'end_time' => $end_time
        ];
        return $timeArray;
    }


}
